package com.handpay.zztong.hp.withdraw.model;

import com.handpay.framework.LuaTableUtil;
import com.handpay.framework.fastjson.FastJsonHelper;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.base.callback.BusinessEngineListener;
import com.handpay.zztong.hp.base.model.base.BaseModel;
import com.handpay.zztong.hp.bean.WithdrawRecordsBill;
import com.handpay.zztong.hp.config.HttpUrlConstant;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.withdraw.bean.QueryWithdrawRecordBean;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import se.krka.kahlua.vm.LuaTable;

/**
 * Created by fplei on 2017/5/3.
 */

public class QueryWithdrawRecordModel extends BaseModel {
    private BusinessEngineListener mBusinessEngineListener;

    public boolean doQueryWithdrawRecord(ZZTongActivity activity, Hashtable<String, String> params, BusinessEngineListener listener) {
        mBusinessEngineListener = listener;
        return doPost(activity, HttpUrlConstant.Do_NewQueryRWDRecords, params, true, true);
    }

    @Override
    public boolean netResponse(String action, byte[] data) {
        boolean bResult = super.netResponse(action, data);
        if (bResult) {
            return true;
        }
        if (action.equals(HttpUrlConstant.Do_NewQueryRWDRecords)) {
            QueryWithdrawRecordBean bean = new QueryWithdrawRecordBean();
            if (data_type == DATA_TYPE.DATA_TYPE_JSON) {
                try {
                    String result = new String(data, "UTF-8");
                    bean = FastJsonHelper.getObject(result, QueryWithdrawRecordBean.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    LuaTable luaTable = LuaTableUtil.stringToLuaTable(data, true);
                    // 总笔数
                    String items = CommonUtils.objectToString(luaTable.rawget("items"));
                    bean.setItems(items);
                    // 总页数
                    String totalPages = CommonUtils.objectToString(luaTable.rawget("totalPages"));
                    bean.setTotalPages(totalPages);
                    String rwdAmount = CommonUtils.objectToString(luaTable.rawget("applyAmounts"));
                    bean.setApplyAmounts(rwdAmount);
                    String wdAmount = CommonUtils.objectToString(luaTable.rawget("amounts"));
                    bean.setAmounts(wdAmount);
                    LuaTable list = (LuaTable) luaTable.rawget("list");
                    Vector<LuaTable> dataList = LuaTableUtil.getArray(list);
                    if (list != null) {
                        List<  QueryWithdrawRecordBean.WithdrawBody > bills = new ArrayList<>();
                        for (LuaTable billData : dataList) {
                            QueryWithdrawRecordBean.WithdrawBody bill = new QueryWithdrawRecordBean.WithdrawBody();
                            bill.setRwdTime(CommonUtils.objectToString(billData.rawget("rwdTime")));
                            bill.setRwdStatus(CommonUtils.objectToString(billData.rawget("rwdStatus")));
                            bill.setRwdDes(CommonUtils.objectToString(billData.rawget("rwdDes")));
                            bill.setWdMoney(CommonUtils.objectToString(billData.rawget("rwdMoney")));
                            if (WithdrawRecordsBill.STATUS_WD_SUCCESS == CommonUtils.objectToInt(bill.getRwdStatus())) {
                                bill.setWdMoney(CommonUtils.objectToString(billData.rawget("wdMoney")));
                            }
                            bills.add(bill);
                        }
                        bean.setList(bills);
                    }
                } catch (Exception e) {
                    HPLog.e(HPLog.TAG_RESULT, e.toString());
                }
            }
            if (mBusinessEngineListener != null) {
                mBusinessEngineListener.callbackBusinessResponse(bean);
            }
        }
        return true;
    }
}
